/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.controller;

import java.awt.event.ActionEvent;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.controller.ImageActions;
import offset.nodes.client.editor.model.ImageModel;
import offset.nodes.client.editor.model.Range;
import offset.nodes.client.editor.view.image.ImageDialog;
import offset.nodes.client.editor.view.image.LocalFileInfo;
import offset.nodes.client.veditor.model.VirtualImageModel;
import offset.nodes.client.veditor.view.image.VirtualImageDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class VirtualImageActions extends ImageActions {

    public static class VirtualInsertImageAction extends InsertImageAction {

        public VirtualInsertImageAction(Editor editor) {
            super(editor);
        }
        
        protected VirtualImageModel getVirtualModel() {
            return new VirtualImageModel(getDocumentContext(), getEditor());
        }        

        @Override
        public void action(ActionEvent e) throws Exception {
            VirtualImageDialog dialog = new VirtualImageDialog(getEditor());
            dialog.setLocation(100, 100);
            if (dialog.showDialog() == ImageDialog.CANCEL)
                return;

            switch (dialog.getImageType()) {
                case ImageDialog.TYPE_REPOSITORY:
                    getVirtualModel().insertRepositoryImage(new Range(getEditor().getBrowserPane().getSelectionStart(),
                            getEditor().getBrowserPane().getSelectionEnd()), dialog.getRepositoryUuid());
                    break;

                case ImageDialog.TYPE_LOCAL:
                    LocalFileInfo localFile = dialog.getLocalFile();
                    String imageUuid = uploadImage(localFile);
                    getVirtualModel().insertLocalImage(new Range(getEditor().getBrowserPane().getSelectionStart(),
                            getEditor().getBrowserPane().getSelectionEnd()), imageUuid);
                    break;

                case VirtualImageDialog.TYPE_DATA:
                    getVirtualModel().insertDataImage(new Range(getEditor().getBrowserPane().getSelectionStart(),
                            getEditor().getBrowserPane().getSelectionEnd()), dialog.getPropertyName());
                    break;
            }
        }
    }
}
